home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / +system+ / tools / sound / ahi / developer / devloper.lzx / examples / Device / PlayTest / PlayTest.c < prev    next >
C/C++ Source or Header  |  1979-10-21  |  4KB  |  147 lines

  1. /*
  2. ** This program uses the device interface to play a sampled sound.
  3. ** The input is read from THE DEFAULT INPUT, make sure you
  4. ** start it with "PlayTest pri < mysample.raw" !
  5. ** Where pri is a number from -128 to +127 (may be omitted)
  6. ** The sample should be 8 bit signed, mono (see TYPE).
  7. */
  8.  
  9. #include <devices/ahi.h>
  10. #include <dos/dosasl.h>
  11. #include <exec/memory.h>
  12. #include <proto/exec.h>
  13. #include <proto/dos.h>
  14. #include <proto/ahi.h>
  15. #include <stdlib.h>
  16.  
  17. #define FREQUENCY  8000
  18. #define TYPE       AHIST_M8S
  19. #define BUFFERSIZE 20000
  20.  
  21. struct MsgPort    *AHImp     = NULL;
  22. struct AHIRequest *AHIio     = NULL;
  23. struct AHIRequest *AHIio2    = NULL;
  24. BYTE               AHIDevice = -1;
  25.  
  26. __far BYTE buffer1[BUFFERSIZE];
  27. __far BYTE buffer2[BUFFERSIZE];
  28.  
  29. void cleanup(LONG rc)
  30. {
  31.   if(!AHIDevice)
  32.     CloseDevice((struct IORequest *)AHIio);
  33.   DeleteIORequest((struct IORequest *)AHIio);
  34.   DeleteIORequest((struct IORequest *)AHIio2);
  35.   DeleteMsgPort(AHImp);
  36.   exit(rc);
  37. }
  38.  
  39. void main(int argc, char *argv[])
  40. {
  41.   BYTE *p1=buffer1,*p2=buffer2;
  42.   void *tmp;
  43.   ULONG signals,length;
  44.   struct AHIRequest *link = NULL;
  45.   LONG priority = 0;
  46.   BYTE pri;
  47.  
  48.   if(argc == 2)
  49.   {
  50.     StrToLong(argv[1], &priority);
  51.   }
  52.   pri = priority;
  53.   Printf("Sound priority: %ld\n", pri);
  54.  
  55.   if(AHImp=CreateMsgPort()) {
  56.     if(AHIio=(struct AHIRequest *)CreateIORequest(AHImp,sizeof(struct AHIRequest))) {
  57.       AHIio->ahir_Version = 4;
  58.       AHIDevice=OpenDevice(AHINAME,0,(struct IORequest *)AHIio,NULL);
  59.     }
  60.   }
  61.  
  62.   if(AHIDevice) {
  63.     Printf("Unable to open %s/0 version 4\n",AHINAME);
  64.     cleanup(RETURN_FAIL);
  65.   }
  66.  
  67. // Make a copy of the request (for double buffering)
  68.   AHIio2 = AllocMem(sizeof(struct AHIRequest), MEMF_ANY);
  69.   if(! AHIio2) {
  70.     cleanup(RETURN_FAIL);
  71.   }
  72.   CopyMem(AHIio, AHIio2, sizeof(struct AHIRequest));
  73.  
  74.   SetIoErr(0);
  75.  
  76.   for(;;) {
  77.  
  78. // Fill buffer
  79.     length = Read(Input(),p1,BUFFERSIZE);
  80.  
  81. // Play buffer
  82.     AHIio->ahir_Std.io_Message.mn_Node.ln_Pri = pri;
  83.     AHIio->ahir_Std.io_Command  = CMD_WRITE;
  84.     AHIio->ahir_Std.io_Data     = p1;
  85.     AHIio->ahir_Std.io_Length   = length;
  86.     AHIio->ahir_Std.io_Offset   = 0;
  87.     AHIio->ahir_Frequency       = FREQUENCY;
  88.     AHIio->ahir_Type            = TYPE;
  89.     AHIio->ahir_Volume          = 0x10000;          // Full volume
  90.     AHIio->ahir_Position        = 0x8000;           // Centered
  91.     AHIio->ahir_Link            = link;
  92.     SendIO((struct IORequest *) AHIio);
  93.  
  94.     if(link) {
  95.  
  96. // Wait until the last buffer is finished (== the new buffer is started)
  97.       signals=Wait(SIGBREAKF_CTRL_C | (1L << AHImp->mp_SigBit));
  98.  
  99. // Check for Ctrl-C and abort if pressed
  100.       if(signals & SIGBREAKF_CTRL_C) {
  101.         SetIoErr(ERROR_BREAK);
  102.         break;
  103.       }
  104.  
  105. // Remove the reply and abort on error
  106.       if(WaitIO((struct IORequest *) link)) {
  107.         SetIoErr(ERROR_WRITE_PROTECTED);
  108.         break;
  109.       }
  110.     }
  111.  
  112. // Check for end-of-sound, and wait until it is finished before aborting
  113.     if(length != BUFFERSIZE) {
  114.       WaitIO((struct IORequest *) AHIio);
  115.       break;
  116.     }
  117.  
  118.     link = AHIio;
  119.  
  120. // Swap buffer and request pointers, and restart
  121.     tmp    = p1;
  122.     p1     = p2;
  123.     p2     = tmp;
  124.  
  125.     tmp    = AHIio;
  126.     AHIio  = AHIio2;
  127.     AHIio2 = tmp;
  128.   }
  129.   
  130.  
  131. // Abort any pending iorequests
  132.   AbortIO((struct IORequest *) AHIio);
  133.   WaitIO((struct IORequest *) AHIio);
  134.  
  135.   if(link) { // Only if the second request was started
  136.     AbortIO((struct IORequest *) AHIio2);
  137.     WaitIO((struct IORequest *) AHIio2);
  138.   }
  139.  
  140.   if(IoErr()) {
  141.     PrintFault(IoErr(), argv[0] );
  142.     cleanup(RETURN_ERROR);
  143.   }
  144.  
  145.   cleanup(RETURN_OK);
  146. }
  147.